﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/*
This control is responsible for display member information in admin
*/
public partial class MemberInfoList : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void DropDownList_Dpt_Load(object sender, EventArgs e)
    {
    }
    protected void GridView_Staff_DataBinding(object sender, EventArgs e)
    {
       
    }
    protected void GridView_Staff_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected string GetDptNameById(object DptId)
    {
        DepartmentDb dptDb = new DepartmentDb();

        return (string)dptDb.GetDepartmentNameById((int)DptId) ;
    }

    protected void  GridView_Staff_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //Check if the evaluator and verifier's data is valid, if invalid output error information
        //int EvaluatorSn = ((DictionaryEntry)e.NewValues["Evaluator"].Value;
        int EvaluatorSn = int.Parse((string)e.NewValues["Evaluator"]);
        int VerifierSn = int.Parse((string)e.NewValues["Verifier"]);
        bool Result = StaffDB.Singleton().IsSerialNumberExist(EvaluatorSn);
        if(!Result && EvaluatorSn != 0)
        {
            //output error information
            Utility.DisplayErrorLabel("考核人不存在，请从新输入正确的考核编号。 如果没有，输入0");
            e.Cancel = true;
            return;
        }
        Result = StaffDB.Singleton().IsSerialNumberExist(VerifierSn);
        if (!Result && VerifierSn !=0)
        {
            //output err information
            Utility.DisplayErrorLabel("审核人不存在，请从新输入正确的考核编号。 如果没有，输入0");
            e.Cancel = true;
            return;
        }

        DropDownList Dpt = (DropDownList)(GridView_Staff.Rows[e.RowIndex].FindControl("DropDownList_StaffDpt"));
        e.NewValues.Add("DepartmentNum", Dpt.SelectedValue);
        DropDownList level = (DropDownList)(GridView_Staff.Rows[e.RowIndex].FindControl("DropDownList1"));
        e.NewValues.Add("Level", level.SelectedValue);
        
    }


    protected void DetailsView_Staff_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        //Check if the 
        if(Utility.HasNullValue(e.Values))
        {
            ((Label)Page.Master.FindControl("ErrorLabel")).Text = "输入不能为空！";
            e.Cancel = true;
            return;
        };
        string serialNum = e.Values["SerialNumber"].ToString();
        //Check if the serialNum is valid
        int result = 0;
        if (int.TryParse(serialNum, out result))
        {
            ((Label)Page.Master.FindControl("ErrorLabel")).Text = "";
        }
        else
        {
            ((Label)Page.Master.FindControl("ErrorLabel")).Text = "考核编号无效！";
            e.Cancel = true;
            return;
        }

        StaffDB db = new StaffDB();
        if (db.IsSerialNumberExist(result))
        {
            ((Label)Page.Master.FindControl("ErrorLabel")).Text = "该考核编号已存在！";
            e.Cancel = true;
            return;
        }
        else
        {
            ((Label)Page.Master.FindControl("ErrorLabel")).Text = "";
        }
        DropDownList Dpt = (DropDownList)(DetailsView_Staff.FindControl("DropDownList2"));
        
        e.Values.Add("DepartmentNum", Dpt.SelectedValue);
        DropDownList level = (DropDownList)(DetailsView_Staff.FindControl("DropDownList3"));
        e.Values.Add("Level", level.SelectedValue);
    }

    private void DetailsViewAddField(string Title, string DataField)

    {
        BoundField field = new BoundField();
        field.HeaderText = Title;
        field.DataField = DataField;
        
        DetailsView_Staff.Fields.Add(field);
    }

    private void DetailsViewAddLevelField()
    {
        TemplateField levelField = new TemplateField();
        levelField.HeaderText = "级别";
      
    }

    protected void DetailsView_Staff_ItemCommand(object sender, DetailsViewCommandEventArgs e)
    {
        //Create Fields in command "New"
        DetailsView dv = (DetailsView)sender;
        if(e.CommandName == "New")
        {
            for (int i = dv.Fields.Count - 1; i > 0; --i)
            {
                dv.Fields[i].Visible = true;
            }
        }
        else if (e.CommandName == "Cancel")
        {
            for (int i = dv.Fields.Count - 1; i > 0; --i)
            {
                dv.Fields[i].Visible = false;
            }
        }
        
    }

    protected void DetailsView_Staff_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        DetailsView dv = (DetailsView)sender;
        for (int i = dv.Fields.Count - 1; i > 0; --i)
        {
            dv.Fields[i].Visible = false;
        }
    }
    protected int[] getLevels()
    {
        RewardDB db = new RewardDB();
        RewardDetail[] rewards = db.GetRewards();
        ArrayList levels = new ArrayList();
        for (int i = 0; i < rewards.Length; ++i)
        {
            levels.Add((int)rewards[i].Level);
        }
        return (int[])levels.ToArray(typeof(int));
    }
    protected void DropDownList_Dpt_SelectedIndexChanged(object sender, EventArgs e)
    {
        //get current selected value
        DropDownList dpt = (DropDownList)sender;

        ObjectDataSource_StaffInfo.SelectParameters["DepartmentNum"].DefaultValue = dpt.SelectedValue;
        GridView_Staff.DataBind();
    }

    protected void GridView_Staff_DataBound(object sender, EventArgs e)
    {
        if (GridView_Staff.Rows.Count == 1
            && int.Parse(GridView_Staff.Rows[0].Cells[1].Text) == 0)
        {
            GridView_Staff.Rows[0].Visible = false;
        }
    }

    protected void ObjectDataSource_StaffInfo_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        //Check current selected index to determine the selected method
        if(int.Parse(DropDownList_Dpt.SelectedValue) != 0)
        {
            //ObjectDataSource_StaffInfo.SelectMethod = "GetStaffsByDpt";
            //ObjectDataSource_StaffInfo.SelectParameters.Clear();
            //ObjectDataSource_StaffInfo.SelectParameters.Add("DepartmentNum", DropDownList_Dpt.SelectedValue);
        }

    }
    protected void DropDownList_Dpt_DataBound(object sender, EventArgs e)
    {
        //After data bound, we can add a new item in the dropdown list
        ListItem AllDpt = new ListItem("所有部门", "0");
        DropDownList_Dpt.Items.Insert(0, AllDpt);
    }

    protected void DropDownList2_DataBound(object sender, EventArgs e)
    {
        DropDownList dv = (DropDownList)sender;

        if (DropDownList_Dpt.SelectedIndex != 0)
        {
            dv.SelectedIndex = DropDownList_Dpt.SelectedIndex -1 ;
        }
    }

    protected void GridView_Staff_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)) || (e.Row.RowState == DataControlRowState.Edit))
        {
            for (int i = 0; i < e.Row.Cells.Count; ++i)
            {
                TableCell cell = e.Row.Cells[i];
                int cellWidth = 0;
                foreach (object ctrl in cell.Controls)
                {
                    if (ctrl is TextBox)
                    {
                        TextBox curText = (TextBox)ctrl;
                        int number = 0;
                        if (int.TryParse(curText.Text, out number))
                        {
                            cellWidth = 32;
                            curText.Width = Unit.Pixel(cellWidth);
                        }
                        else
                        {
                            cellWidth = 48;
                            curText.Width = Unit.Pixel(cellWidth);
                        }
                    } 
                    else if (ctrl is DropDownList)
                    {
                        DropDownList curList = (DropDownList)ctrl;
                        if (i == 3)
                            curList.Width = Unit.Pixel(80);
                        else if (i == 5)
                            curList.Width = Unit.Pixel(45);
                    }
                }

                if (cellWidth != 0)
                    cell.Width = Unit.Pixel(cellWidth);
            }
        }
 
        if (e.Row.RowType == DataControlRowType.Header)
        {
            GridView_Staff.Columns[0].ItemStyle.Width = 16;
            GridView_Staff.Columns[1].ItemStyle.Width = 32;
            GridView_Staff.Columns[2].ItemStyle.Width = 48;
            GridView_Staff.Columns[3].ItemStyle.Width = 80;
            GridView_Staff.Columns[4].ItemStyle.Width = 32;
            GridView_Staff.Columns[5].ItemStyle.Width = 48;
            GridView_Staff.Columns[6].ItemStyle.Width = 48;
            GridView_Staff.Columns[7].ItemStyle.Width = 40;
            GridView_Staff.Columns[8].ItemStyle.Width = 40;
        }
    }

    protected void DropDownList_StaffDpt_DataBound(object sender, EventArgs e)
    {
        DropDownList dpt = (DropDownList)sender;
        dpt.SelectedIndex = (int)DataBinder.Eval(((GridViewRow)(dpt.Parent.Parent)).DataItem, "DepartmentNum") -1 ;

    }

    protected void DropDownList1_DataBound(object sender, EventArgs e)
    {
        DropDownList dpt = (DropDownList)sender;
        dpt.SelectedIndex = (int)DataBinder.Eval(((GridViewRow)(dpt.Parent.Parent)).DataItem, "Level") - 1;
    }

    protected string GetPositionName(object Position)
    {
        int Pos = (int)Position;
        if (Pos == 0)
            return StaffDB.PositionNames[Pos];
        else
            return StaffDB.PositionNames[Pos-1];
    }
    protected void DetailsView_Staff_ItemUting(object sender, DetailsViewUpdateEventArgs e)
    {
      
    }
}
